package com.digital.herbarium.service;

import com.digital.herbarium.Entity.PopularRead;
import com.digital.herbarium.Entity.ReadOrder;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.PopularReadMapper;
import com.digital.herbarium.Util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PopularReadService {

    @Autowired
    PopularReadMapper popularReadMapper;

    public void addPopularRead(PopularRead popularRead){
        if(popularRead.getPid() == null || popularRead.getPid() == 0){
            throw  new CustomException(400,"缺少科普信息");
        }
//        if(popularRead.getUserid() == null || popularRead.getUserid() == 0){
//            throw  new CustomException(400,"缺少用户信息id");
//        }
        popularRead.setReadTime(TimeUtil.getNow());

        Integer countpr = popularReadMapper.getPopularReadByPid(Long.valueOf(popularRead.getPid()));
        if (countpr > 0){
            popularReadMapper.editPopularRead(popularRead);

        }else{
            popularReadMapper.addPopularRead(popularRead);
        }

    }

    public List<ReadOrder> getReadOrder(){
        return popularReadMapper.getReadOrder();
    }
}
